package top.fullj.remoting;

import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author bruce.wu
 * @since 2022/2/14 15:50
 */
public class ContextBase implements Context {

    private final Map<String, Object> props;

    public ContextBase() {
        this.props = new ConcurrentHashMap<>();
    }

    @Override
    public boolean hasAttr(@Nonnull String key) {
        return props.containsKey(key);
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T> T getAttr(@Nonnull String key) {
        return (T) props.get(key);
    }

    @Override
    public Object setAttr(@Nonnull String key, @Nonnull Object value) {
        return props.put(key, value);
    }

    @Override
    public Object remove(@Nonnull String key) {
        return props.remove(key);
    }

    @Nonnull
    @Override
    public Map<String, ?> toMap() {
        return Collections.unmodifiableMap(props);
    }

}
